$(function () {
    obj.init();

});

var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        type:null,
        startDom:null,
        endDom:null,
        init:function () {
            var arr= location.pathname.split('/');
            this.type      = arr[arr.length-1];
            this.startDom  = $('#List_Search_StartTime');
            this.endDom    = $('#List_Search_EndTime');
            this.dataGrid  = new xhDataGrid($('#list'), 'follow/getFollow',{type:this.type,startTime:startTime,endTime:endTime});
            this.dataGrid.parseToolbar();
            this.startDom.datebox('setValue', startTime);
            this.endDom.datebox('setValue', endTime);
            this.dataGrid.init();
            xhTool.bindDocKeyDown($('#bt_list_search'));
            this.listener();

        },
        listener:function () {
            var $this = this;

            //搜索
            $('#bt_list_search').on('click',function () {
                $this.funObj().search();
            });


            $('#bt_list_see').on('click',function () {
                $this.funObj().detailView();
            });

            $this.funObj().bindDataGridEvent();

            if ($this.type == 1) {
                $this.funObj().AreaSelectEvent();
            } else if($this.type == 2 ) {
                $this.funObj().groupSelectEvent(); 
            }

        },
        funObj:function () {
            var $this = this;
            return {
                search:function () {
                    $this.dataGrid.reload($this.funObj().getParams());
                },
                getParams:function () {
                    var key       = xhExtra.getValue($('#list_search'));
                    var startTime = xhExtra.getValue($('#List_Search_StartTime'));
                    var endTime   = xhExtra.getValue($('#List_Search_EndTime'));
                    var type      = $this.type;
                    if (type == 1 ) {
                        var select_group    = xhExtra.getValue($('#select_group'));
                        var select_province = $('#select_province').combobox('getText');
                        var select_city     = $('#select_city').combobox('getText');
                        var select_county   = $('#select_county').combobox('getText');
                        return {key:key,startTime:startTime,endTime:endTime,type:type,group:select_group,
                            province:select_province,city:select_city,county:select_county};
                    } else if (type == 2 ) {
                        var select_group       = xhExtra.getValue($('#select_group'));
                        var select_innerGroup =  xhExtra.getValue($('#select_innerGroup'));
                        return {key:key,startTime:startTime,endTime:endTime,type:type,group:select_group,innerGroup:select_innerGroup};
                    }
                },
                detailView:function () {

                    var startTime = xhExtra.getValue($('#List_Search_StartTime'));
                    var endTime   = xhExtra.getValue($('#List_Search_EndTime'));
                    var type      = $this.type;
                    var params    = "?startTime="+startTime+"&endTime="+endTime;
                    if(type == 1){ //学校
                        var cuId  = $this.dataGrid.getSelectValue('cuId');
                        params += "&cuId="+cuId;
                    }else if(type ==2){ //人员
                        var userId  = $this.dataGrid.getSelectValue('userId');
                        params += "&userId="+userId;
                    }

                    window.top.xhTop.showModal({
                        title: '查看信息',
                        content: '<iframe  src="'+baseUrl+'follow/followDetail'+params+'"  style="width: 100%;;border-style: none;padding: 0px;height:500px"  scrolling="yes"   ></iframe>',
                        width:900,
                        backClose:false
                    });

                },
                bindDataGridEvent:function () {
                    $this.dataGrid.onDblClickRow = function (rowIndex,rowData) {
                        var startTime = xhExtra.getValue($('#List_Search_StartTime'));
                        var endTime   = xhExtra.getValue($('#List_Search_EndTime'));
                        var type      = $this.type;
                        var params    = "?startTime="+startTime+"&endTime="+endTime;
                        if(type == 1){ //学校
                            var cuId  = rowData['cuId'];
                            params += "&cuId="+cuId;
                        }else if(type ==2){ //人员
                            var userId  = rowData['userId'];
                            params += "&userId="+userId;
                        }
                        window.top.xhTop.showModal({
                            title: '查看信息',
                            content: '<iframe  src="'+baseUrl+'follow/followDetail'+params+'"  style="width: 100%;;border-style: none;padding: 0px;height:500px"  scrolling="yes"   ></iframe>',
                            width:900,
                            backClose:false,
                        });
                    };
                    if ( $this.type == 2) {
                        $this.dataGrid.rowStyler = function(rowIndex, rowData){
                            if(rowData.num != 0 ){
                                $('tr[datagrid-row-index="'+rowIndex+'"]').find('td[field="userName"]').css("color","#2baab1");
                            }
                        }
                    }
                },
                AreaSelectEvent:function () {
                    $('#select_province').combobox({
                        onSelect: function (record) {
                            if (record.value != '') {
                                $('#select_city').combobox({
                                    url: baseUrl + 'customer/getAreaById/' + record.value,
                                    valueField: 'areaCode',
                                    textField: 'areaName',
                                    onSelect: function (record2) {
                                        if (record2.areaCode != '') {
                                            $('#select_county').combobox({
                                                url: baseUrl + 'customer/getAreaById/' + record2.areaCode,
                                                valueField: 'areaCode',
                                                textField: 'areaName'
                                            })
                                        } else {
                                            $('#select_county').combobox('setValue', '');
                                        }
                                    }
                                })
                            } else {
                                $('#select_city').combobox('setValue', '');
                                $('#select_county').combobox('setValue', '');
                            }
                        }
                    });
                },
                groupSelectEvent:function () {
                    $('#select_group').combobox({
                        onSelect: function (record) {
                            $('#select_innerGroup').combobox({
                                url: baseUrl + 'follow/getSCBLevel/' + record.value,
                                valueField: 'id',
                                textField: 'text'
                            });
                        }
                    });
                }
            }
        }
    }
};